﻿using System;
using System.Web;
using Microsoft.AspNet.SignalR;
using System.Threading.Tasks;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using DataCenter;

namespace MvcApplicationSignalR.Hubs
{
    /// <summary>
    /// SignalR hub tương tác với máy client máy in và ajax in
    /// </summary>
    public class PrinterHub : HubBase
    {
        static ConcurrentDictionary<string, string> dict = new ConcurrentDictionary<string, string>();
        const string WEB_CLIENT = "Web Client";
        const string WIN_CLIENT = "Win Client";
        
        /// <summary>
        /// In dữ liệu,  In dữ liệu, nhận lệnh in từ web client
        /// </summary>
        /// <param name="clientName"></param>
        /// <param name="printerName"></param>
        /// <param name="type"></param>
        public void Print(string clientName, string printerName, string type)
        {
            // Gọi hàm in của client
            try
            {
                Client client = _ClientList.FindByName(WIN_CLIENT);
                if (client != null)
                {
                    Clients.Client(client.ConnectionID).printDocument(printerName);
                }
                else
                {
                    Clients.Caller.onError(clientName, printerName, "Error: Client not found");
                }
            }
            catch
            {
                Clients.Caller.onError(clientName, printerName, "Error: Client not found or printer not found");
            }
            // Clients.Client(dic[to]).broadcastMessage(name, message);
        }

        public void sendPrinterNames(string clientName)
        {
            Client client = _ClientList.FindByName(WIN_CLIENT);
            if (client != null)
            {
                Clients.Client(client.ConnectionID).getPrinters(clientName);
            }
            else
            {
                Clients.Caller.onError(clientName, "", "Error: Client not found");
            }

             
        }

        /// <summary>
        /// Xử lý khi in KHÔNG thành công, nhận lệnh in từ win client
        /// </summary>
        /// <param name="clientName">Tên client</param>
        /// <param name="printerName">Tên máy in</param>
        public void PrintError(string clientName, string printerName)
        {
            // Gửi thông báo (gọi hàm) web client
            Clients.All.onError(clientName, printerName);
        }

        /// <summary>
        /// Xử lý khi in thành công, nhận lệnh in từ win client
        /// </summary>
        /// <param name="clientName">Tên client</param>
        /// <param name="printerName">Tên máy in</param>
        public void PrintSuccess(string clientName, string printerName)
        {
            // Gửi thông báo (gọi hàm) web client
            Clients.All.onSuccess(clientName, printerName);
        }

        /// <summary>
        /// Nhận danh sách máy in từ win client
        /// </summary>
        /// <param name="clientName">Tên client</param>
        /// <param name="printers">Danh sách tên máy in</param>
        public void RecievePrinterNames(string clientName, List<string> printers)
        {
            PrinterData pd = new PrinterData();

            pd.SavePrinters(printers, clientName);
            Client client = _ClientList.FindByName(WEB_CLIENT);
            if (client != null)
            {
                Clients.Client(client.ConnectionID).getPrinterNames(pd.GetPrinters());
            }
            else
            {
                Clients.Caller.OnError("Error: Web Client not found");
            }
        }        
    }

}